<!--
    Copyright 2010 Justin Lipton, Jonathan Rosenberg
    
    This file is part of MediaList.

    MediaList is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MediaList is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MediaList.  If not, see <http://www.gnu.org/licenses/>.
-->
<html>
<head>
<script type="text/javascript" src="incProg.js"></script>
<script type="text/javascript" src="delShow.js"></script>
<script type="text/javascript" src="changeRating.js"></script>
<script type="text/javascript" src="changeProgress.js"></script>
</head>
<?php 
    echo "<div name=\"here\"></div>";
    $currentUser=$_GET["currentUser"];
    if (isset($_COOKIE["user"])&& $currentUser==$_COOKIE["user"])
    {
        $con = mysql_connect("localhost", "root");
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("DB", $con);
    
        echo "Welcome " . $_COOKIE["user"] . "! (<a href=logout.php>Logout</a>)<br />";
        
        echo "<a href=addShow.php>Add Show</a><br/>";
        
        echo "<a href=search.php>Search</a><br/>";
        
        echo "<a href=addNewShow.php>Add a new show</a><br/>";
        
        echo "This is your list<br/>";
        
        $sql= "SELECT * FROM ".$currentUser." WHERE Status='W'";
        $result = mysql_query($sql, $con);
        
        echo "Watching:
        <table border='1'>
        <tr>
        <th>Name</th>
        <th>Current Episode</th>
        <th>Inc</th>
        <th>Rating</th>
        <th>Delete</th>
        <th>Move</th>
        </tr>";
        while($row = mysql_fetch_array($result))
        {
            echo "<tr id=\"".$row['ShowId']."\">";
            echo "<td><a href=show.php?showID=" . $row['ShowId'] . ">". $row['ShowName'] ."</a></td>";
            echo "<td> <div id=changeProgress".$row['ShowId']."><a href=\"javascript:void(0);\" title=\"Click to edit progress\" 
                onclick=\"ProgressInput('".$_COOKIE["user"]."', ".$row['ShowId'].");\">"
                .$row['Progress']."</a> </div></td>";
            echo "<td> <a href=\"javascript:void(0);\" title=\"Click to increase episode number by 1\" 
                onclick=\"incProg('".$_COOKIE["user"]."', '".$row['ShowId']."');\">+</a> </td>";
            echo "<td> <div id=changeRating".$row['ShowId']."><a href=\"javascript:void(0);\" title=\"Click to edit rating\" 
                onclick=\"RatingInput('".$_COOKIE["user"]."', ".$row['ShowId'].");\">";
                if($row['Rating']==NULL)
                {
                    echo "--</a> </div></td>";
                }
                else
                {
                    echo $row['Rating']."</a> </div></td>";
                }
            echo "<td> <input type=\"button\" value=\"Delete\"
              onclick=\"delShow('".$_COOKIE["user"]."', '".$row['ShowId']."')\" /> </td>";     
            echo "<td> <form> <select name=\"status\" size=\"1\">
                <option value=\"C\">Completed</option>
                <option value=\"D\">Dropped</option> 
                <option value=\"P\">Planning</option></select>
                <input type=\"button\" value=\"Move\" onclick=\"changeStatus('W', status.value, ".
                $row['ShowId'].")\"></form>
                </td>";
            echo "</tr>";
        }
        echo "</table>";   
        echo "<br/>";
        
        $sql= "SELECT * FROM ".$currentUser." WHERE Status='C'";
        $result = mysql_query($sql, $con);
        
        echo "Completed:
        <table border='1'>
        <tr>
        <th>Name</th>
        <th>Total Episodes</th>
        <th>Rating</th>
        <th>Delete</th>
        <th>Move</th>
        </tr>";
        while($row = mysql_fetch_array($result))
        {
            echo "<tr id=\"".$row['ShowId']."\">";
            echo "<td><a href=show.php?showID=" . $row['ShowId'] . ">". $row['ShowName'] ."</a></td>";
            echo "<td ><div name=\"P".$row['ShowId']."\">" . $row['Progress'] . "</div></td>";
            echo "<td> <div id=changeRating".$row['ShowId']."><a href=\"javascript:void(0);\" title=\"Click to edit rating\" 
                onclick=\"RatingInput('".$_COOKIE["user"]."', ".$row['ShowId'].");\">";
                if($row['Rating']==NULL)
                {
                    echo "--</a> </div></td>";
                }
                else
                {
                    echo $row['Rating']."</a> </div></td>";
                }
            echo "<td> <input type=\"button\" value=\"Delete\"
              onclick=\"delShow('".$_COOKIE["user"]."', '".$row['ShowId']."')\" /> </td>";   
            echo "<td> <form> <select name=\"status\" size=\"1\">
                <option value=\"W\">Watching</option>
                <option value=\"D\">Dropped</option> 
                <option value=\"P\">Planning</option></select>
                <input type=\"button\" value=\"Move\" onclick=\"changeStatus('C', status.value, ".
                $row['ShowId'].")\"></form>
                </td>";
            echo "</tr>";
        }
        echo "</table>";   
        echo "<br/>";
    
        $sql= "SELECT * FROM ".$currentUser." WHERE Status='D'";
        $result = mysql_query($sql, $con);
        
        echo "Dropped:
        <table border='1'>
        <tr>
        <th>Name</th>
        <th>Current Episode</th>
        <th>Rating</th>
        <th>Delete</th>
        <th>Move</th>
        </tr>";
        while($row = mysql_fetch_array($result))
        {
            echo "<tr id=\"".$row['ShowId']."\">";
            echo "<td><a href=show.php?showID=" . $row['ShowId'] . ">". $row['ShowName'] ."</a></td>";
            echo "<td> <div id=changeProgress".$row['ShowId']."><a href=\"javascript:void(0);\" title=\"Click to edit progress\" 
                onclick=\"ProgressInput('".$_COOKIE["user"]."', ".$row['ShowId'].");\">"
                .$row['Progress']."</a> </div></td>";
            echo "<td> <div id=changeRating".$row['ShowId']."><a href=\"javascript:void(0);\" title=\"Click to edit rating\" 
                onclick=\"RatingInput('".$_COOKIE["user"]."', ".$row['ShowId'].");\">";
                if($row['Rating']==NULL)
                {
                    echo "--</a> </div></td>";
                }
                else
                {
                    echo $row['Rating']."</a> </div></td>";
                }
            echo "<td> <input type=\"button\" value=\"Delete\"
              onclick=\"delShow('".$_COOKIE["user"]."', '".$row['ShowId']."')\" /> </td>";    
            echo "<td> <form> <select name=\"status\" size=\"1\">
                <option value=\"W\">Watching</option>
                <option value=\"C\">Completed</option>
                <option value=\"P\">Planning</option> </select>
                <input type=\"button\" value=\"Move\" onclick=\"changeStatus('D', status.value, ".
                $row['ShowId'].")\"></form>
                </td>";
            echo "</tr>";
        }
        echo "</table>";   
        echo "<br/>";
        
        $sql= "SELECT * FROM ".$currentUser." WHERE Status='P'";
        $result = mysql_query($sql, $con);
        
        echo "Planning:
        <table border='1'>
        <tr>
        <th>Name</th>
        <th>Rating</th>
        <th>Delete</th>
        <th>Move</th>
        </tr>";
        while($row = mysql_fetch_array($result))
        {
            echo "<tr id=\"".$row['ShowId']."\">";
            echo "<td><a href=show.php?showID=" . $row['ShowId'] . ">". $row['ShowName'] ."</a></td>";
            echo "<td> <div id=changeRating".$row['ShowId']."><a href=\"javascript:void(0);\" title=\"Click to edit rating\" 
                onclick=\"RatingInput('".$_COOKIE["user"]."', ".$row['ShowId'].");\">";
                if($row['Rating']==NULL)
                {
                    echo "--</a> </div></td>";
                }
                else
                {
                    echo $row['Rating']."</a> </div></td>";
                }
            echo "<td> <input type=\"button\" value=\"Delete\"
              onclick=\"delShow('".$_COOKIE["user"]."', '".$row['ShowId']."')\" /> </td>";   
            echo "<td> <form> <select name=\"status\" size=\"1\">
                <option value=\"W\">Watching</option>
                <option value=\"C\">Completed</option>
                <option value=\"D\">Dropped</option> </select>
                <input type=\"button\" value=\"Move\" onclick=\"changeStatus('P', status.value, ".
                $row['ShowId'].")\"></form>
                </td>";
            echo "</tr>";
        }
        echo "</table>";   
        echo "<br/>";
    }  
    else if(isset($_COOKIE["user"]))
    {
        $con = mysql_connect("localhost", "root");
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("DB", $con);
    
        echo "Welcome " . $_COOKIE["user"] . "! (<a href=logout.php>Logout</a>)<br />";
        
        echo "You are currently viewing ".$currentUser."'s list<br/>";
        
        $sql= "SELECT * FROM ".$currentUser." WHERE Status='W'";
        $result = mysql_query($sql, $con);
        
        echo "Watching:
        <table border='1'>
        <tr>
        <th>Name</th>
        <th>Current Episode</th>
        <th>Rating</th>
        </tr>";
        while($row = mysql_fetch_array($result))
        {
            echo "<tr id=\"".$row['ShowId']."\">";
            echo "<td><a href=show.php?showID=" . $row['ShowId'] . ">". $row['ShowName'] ."</a></td>";
            echo "<td ><div name=\"P".$row['ShowId']."\">" . $row['Progress'] . "</div></td>";
            if($row['Rating']==NULL)
            {
                echo "<td>--</td>";    
            }
            else
            {
                echo "<td>" . $row['Rating'] . "</td>";    
            }
            echo "</tr>";
        }
        echo "</table>";     
        echo "<br/>";
        
        $sql= "SELECT * FROM ".$currentUser." WHERE Status='C'";
        $result = mysql_query($sql, $con);
        
        echo "Completed:
        <table border='1'>
        <tr>
        <th>Name</th>
        <th>Total Episodes</th>
        <th>Rating</th>
        </tr>";
        while($row = mysql_fetch_array($result))
        {
            echo "<tr id=\"".$row['ShowId']."\">";
            echo "<td><a href=show.php?showID=" . $row['ShowId'] . ">". $row['ShowName'] ."</a></td>";
            echo "<td ><div name=\"P".$row['ShowId']."\">" . $row['Progress'] . "</div></td>";
            if($row['Rating']==NULL)
            {
                echo "<td>--</td>";    
            }
            else
            {
                echo "<td>" . $row['Rating'] . "</td>";    
            }            
            echo "</tr>";
        }
        echo "</table>";   
        echo "<br/>";
        
        $sql= "SELECT * FROM ".$currentUser." WHERE Status='D'";
        $result = mysql_query($sql, $con);
        
        echo "Dropped:
        <table border='1'>
        <tr>
        <th>Name</th>
        <th>Current Episode</th>
        <th>Rating</th>
        </tr>";
        while($row = mysql_fetch_array($result))
        {
            echo "<tr id=\"".$row['ShowId']."\">";
            echo "<td><a href=show.php?showID=" . $row['ShowId'] . ">". $row['ShowName'] ."</a></td>";
            echo "<td ><div name=\"P".$row['ShowId']."\">" . $row['Progress'] . "</div></td>";
            if($row['Rating']==NULL)
            {
                echo "<td>--</td>";    
            }
            else
            {
                echo "<td>" . $row['Rating'] . "</td>";    
            }        
            echo "</tr>";
        }
        echo "</table>";     
        echo "<br/>";
        
        $sql= "SELECT * FROM ".$currentUser." WHERE Status='P'";
        $result = mysql_query($sql, $con);
        
        echo "Planning:
        <table border='1'>
        <tr>
        <th>Name</th>
        <th>Rating</th>
        </tr>";
        while($row = mysql_fetch_array($result))
        {
            echo "<tr id=\"".$row['ShowId']."\">";
            echo "<td><a href=show.php?showID=" . $row['ShowId'] . ">". $row['ShowName'] ."</a></td>";
            if($row['Rating']==NULL)
            {
                echo "<td>--</td>";    
            }
            else
            {
                echo "<td>" . $row['Rating'] . "</td>";    
            }        
            echo "</tr>";
        }
        echo "</table>";     
        echo "<br/>";
    }  
    else
    {
        echo "Please log in to see your list<br/>";
        echo "<a href=login.php>Login</a> <a href=register.php>Register</a>";
    }
?>

</html>